<?php

namespace app\modules\setting\models\cpgl;

use app\models\EtcProductBan;
use app\models\EtcSellerTiktokShop;
use sookie\auth\models\Adminuser;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use yii\db\Expression;
use yii\helpers\ArrayHelper;
use app\modules\setting\models\cpgl\QlUserTiktokProductGoods;
use yii\helpers\Json;

/**
 * QlUserTiktokProductGoodsSearch 是 `app\modules\setting\models\cpgl\QlUserTiktokProductGoods` 的搜索模型.
 */
class QlUserTiktokProductGoodsSearch extends QlUserTiktokProductGoods
{


    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // 绕过scenario()在父类中的实现
        return Model::scenarios();
    }

    /**
 * Creates data provider instance with search query applied
 *
 * @param array $params
 *
 * @param integer $pageSize
 *
 * @return ActiveDataProvider
 */
    public function search($params,$select=null)
    {
        $query = QlUserTiktokProductGoods::find()->alias("t")->select($select ? $select : "*");
        $query->andWhere(new Expression("not exists(select id  from ql_campaign_cn where campaign_id=t.campaign_id)"));

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'pagination' => ['pageSize' => ArrayHelper::getValue($params,"pageSize",10),'defaultPageSize' => 10,'pageSizeParam' => 'pageSize'],
            'sort' => ['defaultOrder' => ['id' => SORT_DESC]],
        ]);

        $this->load($params,"");

        if (!$this->validate()) {
            // 如果您不想在验证失败时返回任何记录，请取消下面一行的注释
            // $query->where('0=1');
            return $dataProvider;
        }

        // 过滤条件
        $query->andFilterWhere([
            'id' => $this->id,
        ]);


        $query->andFilterWhere(['like', 'user_id', $this->user_id])                                ->andFilterWhere(['like', 'company_id', $this->company_id])
            ->andFilterWhere(['like', 'name', $this->name])
            ->andFilterWhere(['like', 'price', $this->price])
            ->andFilterWhere(['like', 'min_price', $this->min_price])
            ->andFilterWhere(['like', 'max_price', $this->max_price])
            ->andFilterWhere(['like', 'creator_commission_percent', $this->creator_commission_percent])
            ->andFilterWhere(['like', 'seller_commission_percent', $this->seller_commission_percent])
            ->andFilterWhere(['like', 'product_id', $this->product_id])
            ->andFilterWhere(['like', 'product_rating', $this->product_rating])
            ->andFilterWhere(['like', 'product_review_count', $this->product_review_count])
            ->andFilterWhere(['like', 'product_sales', $this->product_sales])
            ->andFilterWhere(['like', 'seller_id', $this->seller_id])
            ->andFilterWhere(['like', 'shop_code', $this->shop_code])
            ->andFilterWhere(['like', 'sample_quota', $this->sample_quota])
            ->andFilterWhere(['like', 'content', $this->content])
            ->andFilterWhere(['like', 'img', $this->img])
            ->andFilterWhere(['like', 'link', $this->link])
            ->andFilterWhere(['like', 'product_link', $this->product_link])
            ->andFilterWhere(['like', 'data', $this->data])
            ->andFilterWhere(['like', 'created_at', $this->created_at])
            ->andFilterWhere(['like', 'type', $this->type]);

        return $dataProvider;
    }
    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @param integer $pageSize
     *
     * @return ActiveDataProvider
     */
    public function searchData($params,$select=null)
    {
        $query = QlUserTiktokProductGoods::find()->alias("t")->select($select ? $select : "*");
        $query->andWhere(new Expression("exists(select id  from ql_campaign_cn where campaign_id=t.campaign_id)"));

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'pagination' => ['pageSize' => ArrayHelper::getValue($params,"pageSize",10),'defaultPageSize' => 10,'pageSizeParam' => 'pageSize'],
            'sort' => ['defaultOrder' => ['id' => SORT_DESC]],
        ]);

        $this->load($params,"");

        if (!$this->validate()) {
            // 如果您不想在验证失败时返回任何记录，请取消下面一行的注释
            // $query->where('0=1');
            return $dataProvider;
        }

        // 过滤条件
        $query->andFilterWhere([
            'id' => $this->id,
        ]);


        $query->andFilterWhere(['like', 'user_id', $this->user_id])                                ->andFilterWhere(['like', 'company_id', $this->company_id])
            ->andFilterWhere(['like', 'name', $this->name])
            ->andFilterWhere(['like', 'price', $this->price])
            ->andFilterWhere(['like', 'min_price', $this->min_price])
            ->andFilterWhere(['like', 'max_price', $this->max_price])
            ->andFilterWhere(['like', 'creator_commission_percent', $this->creator_commission_percent])
            ->andFilterWhere(['like', 'seller_commission_percent', $this->seller_commission_percent])
            ->andFilterWhere(['like', 'product_id', $this->product_id])
            ->andFilterWhere(['like', 'product_rating', $this->product_rating])
            ->andFilterWhere(['like', 'product_review_count', $this->product_review_count])
            ->andFilterWhere(['like', 'product_sales', $this->product_sales])
            ->andFilterWhere(['like', 'seller_id', $this->seller_id])
            ->andFilterWhere(['like', 'shop_code', $this->shop_code])
            ->andFilterWhere(['like', 'sample_quota', $this->sample_quota])
            ->andFilterWhere(['like', 'content', $this->content])
            ->andFilterWhere(['like', 'img', $this->img])
            ->andFilterWhere(['like', 'link', $this->link])
            ->andFilterWhere(['like', 'product_link', $this->product_link])
            ->andFilterWhere(['like', 'data', $this->data])
            ->andFilterWhere(['like', 'created_at', $this->created_at])
            ->andFilterWhere(['like', 'type', $this->type]);

        return $dataProvider;
    }


    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @param integer $pageSize
     *
     * @return ActiveDataProvider
     */
    public function searchSeller($params,$select=null)
    {
        $info = Adminuser::getUserInfo();
        $query = QlUserTiktokProductGoods::find()->alias("t")->select($select ? $select : "*");
        $query->andWhere(new Expression("exists(select id  from ql_campaign_cn where campaign_id=t.campaign_id)"));
        $ids = EtcProductBan::find()->select(['id'])->column();
        if($ids){
            $query->andWhere(['not in','id',$ids]);
        }
        $shop_codes = EtcSellerTiktokShop::find()->select(['shop_code'])->andWhere(['seller_id'=>$info->id])->column();
        if(empty($shop_codes)){
            $shop_codes = "0";
        }
        $query->andWhere(['shop_code'=>$shop_codes]);

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'pagination' => ['pageSize' => ArrayHelper::getValue($params,"pageSize",10),'defaultPageSize' => 10,'pageSizeParam' => 'pageSize'],
            'sort' => ['defaultOrder' => ['id' => SORT_DESC]],
        ]);

        $this->load($params,"");

//        if (!$this->validate()) {
//            // 如果您不想在验证失败时返回任何记录，请取消下面一行的注释
//            // $query->where('0=1');
//            return $dataProvider;
//        }

        // 过滤条件
        $query->andFilterWhere([
            'id' => $this->id,
        ]);


        $query->andFilterWhere(['like', 'user_id', $this->user_id])                                ->andFilterWhere(['like', 'company_id', $this->company_id])
            ->andFilterWhere(['like', 'name', $this->name])
            ->andFilterWhere(['like', 'price', $this->price])
            ->andFilterWhere(['like', 'min_price', $this->min_price])
            ->andFilterWhere(['like', 'max_price', $this->max_price])
            ->andFilterWhere(['like', 'creator_commission_percent', $this->creator_commission_percent])
            ->andFilterWhere(['like', 'seller_commission_percent', $this->seller_commission_percent])
            ->andFilterWhere(['like', 'product_id', $this->product_id])
            ->andFilterWhere(['like', 'product_rating', $this->product_rating])
            ->andFilterWhere(['like', 'product_review_count', $this->product_review_count])
            ->andFilterWhere(['like', 'product_sales', $this->product_sales])
            ->andFilterWhere(['like', 'seller_id', $this->seller_id])
            ->andFilterWhere(['like', 'shop_code', $this->shop_code])
            ->andFilterWhere(['like', 'sample_quota', $this->sample_quota])
            ->andFilterWhere(['like', 'content', $this->content])
            ->andFilterWhere(['like', 'img', $this->img])
            ->andFilterWhere(['like', 'link', $this->link])
            ->andFilterWhere(['like', 'product_link', $this->product_link])
            ->andFilterWhere(['like', 'data', $this->data])
            ->andFilterWhere(['like', 'created_at', $this->created_at])
            ->andFilterWhere(['like', 'type', $this->type]);

//        echo $query->createCommand()->rawSql;exit();
        return $dataProvider;
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @param integer $pageSize
     *
     * @return ActiveDataProvider
     */
    public function searchSellerTbcp($params,$select=null)
    {
        $query = QlUserTiktokProductGoods::find()->alias("t")->select($select ? $select : "*");
        $query->andWhere(new Expression("exists(select id  from ql_campaign_cn where campaign_id=t.campaign_id)"));
        $ids = EtcProductBan::find()->select(['id'])->column();
        if($ids){
            $query->andWhere(['not in','id',$ids]);
        }

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'pagination' => ['pageSize' => ArrayHelper::getValue($params,"pageSize",10),'defaultPageSize' => 10,'pageSizeParam' => 'pageSize'],
            'sort' => ['defaultOrder' => ['id' => SORT_DESC]],
        ]);

        $this->load($params,"");

//        if (!$this->validate()) {
//            // 如果您不想在验证失败时返回任何记录，请取消下面一行的注释
//            // $query->where('0=1');
//            return $dataProvider;
//        }

        // 过滤条件
        $query->andFilterWhere([
            'id' => $this->id,
        ]);


        $query->andFilterWhere(['like', 'user_id', $this->user_id])                                ->andFilterWhere(['like', 'company_id', $this->company_id])
            ->andFilterWhere(['like', 'name', $this->name])
            ->andFilterWhere(['like', 'price', $this->price])
            ->andFilterWhere(['like', 'min_price', $this->min_price])
            ->andFilterWhere(['like', 'max_price', $this->max_price])
            ->andFilterWhere(['like', 'creator_commission_percent', $this->creator_commission_percent])
            ->andFilterWhere(['like', 'seller_commission_percent', $this->seller_commission_percent])
            ->andFilterWhere(['like', 'product_id', $this->product_id])
            ->andFilterWhere(['like', 'product_rating', $this->product_rating])
            ->andFilterWhere(['like', 'product_review_count', $this->product_review_count])
            ->andFilterWhere(['like', 'product_sales', $this->product_sales])
            ->andFilterWhere(['like', 'seller_id', $this->seller_id])
            ->andFilterWhere(['like', 'shop_code', $this->shop_code])
            ->andFilterWhere(['like', 'sample_quota', $this->sample_quota])
            ->andFilterWhere(['like', 'content', $this->content])
            ->andFilterWhere(['like', 'img', $this->img])
            ->andFilterWhere(['like', 'link', $this->link])
            ->andFilterWhere(['like', 'product_link', $this->product_link])
            ->andFilterWhere(['like', 'data', $this->data])
            ->andFilterWhere(['like', 'created_at', $this->created_at])
            ->andFilterWhere(['like', 'type', $this->type]);

//        echo $query->createCommand()->rawSql;exit();
        return $dataProvider;
    }
}
